/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Ext.define('Nutrient.view.food.chart.ComponentsDonut',
{
	extend: 'Ext.ux.highcharts.Donut',
	alias: 'widget.nutrient.food.chart.componentsdonut',

	initComponent: function(config)
	{	
		var defaults = {
			plotOptions: {
				series: {
					animation: {
						duration: 600
					},
					dataLabels: {
						style: {
							fontSize: '10px'
						}
					}
				}
			},
			tooltip: {
				useHTML: true,
				valueDecimals: 2,
				formatter: function() {
					if (!this.point.config.visible) return false;
					return this.point.name + ': '+ this.y + ' g';
				}
			},
			coreSerie: {
				innerSize: '20%',
				size: '59%',
				dataLabels: {
					formatter: function() {
						if (this.y < 5) return null;
						return this.point.name;
					},
					color: 'white',
					distance: -30
				}
			},
			wrapperSerie: {
				innerSize: '60%',
				dataLabels: {
					formatter: function() {
						if (this.y < 1) return null;
						return this.point.name;
					}
				}
			}
		};

		this.config = Ext.Object.merge(defaults, this.config);

		this.callParent(arguments);
	},

	update: function(food)
	{
		var data = [{
			name: 'Protéines',
			value: food.protein,
			color: '#AA4643'
		}, {
			name: 'Glucides',
			value: food.carbohydrates,
			color: '#89A54E',
			subdata: [{
				name: 'Amidon',
				value: food.starch
			}, {
				name: 'Sucre',
				value: food.sugar
			}]
		}, {
			name: 'Lipides',
			value: food.fat,
			color: '#DB843D',
			subdata: [{
				name: 'AG saturés',
				value: food.fattyAcidsSaturated
			}, {
				name: 'AG mono-instaurés',
				value: food.fattyAcidsMonounsaturated
			}, {
				name: 'AG poly-instaurés',
				value: food.fattyAcidsPolyunsaturated
			}]
		}, {
			name: 'Eau',
			value: food.water,
			color: '#3D96AE'
		}, {
			name: 'Alcool',
			value: food.alcohol,
			color: '#92A8CD'
		}, {
			name: 'Fibres',
			value: food.fiber,
			color: '#A47D7C'
		}, {
			name: 'Autres',
			value: Math.min(100, Math.round((100 - food.protein - food.carbohydrates - food.fat - food.water - food.alcohol - food.fiber) * 100) / 100),
			color: '#CCCCCC'
		}];

		this.callParent([data]);
	}
});